import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


/**
 * This class creates  an interface for an agency or an Employer
 * Final Project for TCSS 445.
 * 
 * @author Sanjeev Kamboj
 */
@SuppressWarnings("serial")
public class EventPostingPanel extends JPanel {
private static final int NUM_COLUMNS = 20;
	
	private User my_user;
	
	private SqlDao my_dao;

	private JLabel my_type;
	private JLabel my_audience;
	private JLabel my_obj;
	private JLabel my_address;
	private JLabel my_date;
	private JLabel my_time;
	private JLabel my_info;
	
	private JComboBox<String> my_type_field;
	private JTextField my_audience_field;
	private JTextArea my_obj_field;
	private JTextField my_address_field;
	private JTextField my_date_field;
	private JTextField my_time_field;
	private JTextArea my_info_field;

	private JFrame my_frame;
	
	private JButton submit_button;
	private JButton cancel_button;
	
	
	public EventPostingPanel(final JFrame the_frame, final SqlDao the_dao, final User user) {
		super(new BorderLayout());
		
		my_frame = the_frame;
		my_dao = the_dao;
		my_user = user;
		
		//create labels
		my_type = new JLabel("Type of Event: ");
		my_audience = new JLabel("Intended Audience: ");
		my_obj = new JLabel("Objective: ");
		my_address = new JLabel("Address: ");
		my_date = new JLabel("Date (mm/dd/yyyy): ");
		my_time = new JLabel("Time: ");
		my_info = new JLabel("Event Information: ");
 
		String[] types = {"Hiring Event", "Training", "Information Session"};
		my_type_field = new JComboBox<String>(types);
		
	    my_audience_field = new JTextField(NUM_COLUMNS);
		my_obj_field = new JTextArea();
		my_address_field = new JTextField(NUM_COLUMNS);
		my_date_field = new JTextField(NUM_COLUMNS);
		my_time_field = new JTextField(NUM_COLUMNS);
		my_info_field = new JTextArea();
		
		my_info_field.setBorder(BorderFactory.createLineBorder(Color.black));
		my_obj_field.setBorder(BorderFactory.createLineBorder(Color.black));
		
 
        //Tell accessibility tools about label/textfield pairs.
        my_type.setLabelFor(my_type_field);
        my_audience.setLabelFor(my_audience_field);
        my_obj.setLabelFor(my_obj_field);
        my_address.setLabelFor(my_address_field);
        my_date.setLabelFor(my_date_field);
        my_time.setLabelFor(my_time_field);
        my_info.setLabelFor(my_info_field);
        

        //Lay out the labels in a panel.
        JPanel labelPane = new JPanel(new GridLayout(0,1));
        
        labelPane.add(my_type);
        labelPane.add(my_audience);
        labelPane.add(my_obj);
        labelPane.add(my_address);
        labelPane.add(my_date);
        labelPane.add(my_time);
        labelPane.add(my_info);
        
 
        //Layout the text fields in a panel.
        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(my_type_field);
        fieldPane.add(my_audience_field);
        fieldPane.add(my_obj_field);
        fieldPane.add(my_address_field);
        fieldPane.add(my_date_field);
        fieldPane.add(my_time_field);
        fieldPane.add(my_info_field);
 
        //Put the panels in this panel, labels on left,
        //text fields on right.
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(labelPane, BorderLayout.WEST);
        add(fieldPane, BorderLayout.CENTER);
	

        //Layout the text fields in a panel.
        JPanel buttonPane = new JPanel(new FlowLayout());
        submit_button = new JButton("Submit");
        cancel_button = new JButton("Cancel");
        
        submit_button.addActionListener(new SubmitEventListener());
        cancel_button.addActionListener(new ListenCancel());
        
        buttonPane.add(submit_button);
        buttonPane.add(cancel_button);
        
        add(buttonPane, BorderLayout.SOUTH);
	}
	
	   private class SubmitEventListener implements ActionListener{
	        public void actionPerformed(ActionEvent e){
	        	
	        	int select = my_type_field.getSelectedIndex();
	        	String type_str = my_type_field.getItemAt(select);
	        	
	        	my_dao.insertEvent(type_str, 
	        			           my_audience_field.getText(), 
	        			           my_obj_field.getText(), 
	        			           my_address_field.getText(), 
	        			           my_date_field.getText(), 
	        			           my_time_field.getText(), 
	        			           my_info_field.getText());
	        	
	        	my_dao.insertEmpEvent(my_user.getID(), my_dao.getEventAutoCount());
	        	
	        	my_frame.remove(EventPostingPanel.this);
	        	my_frame.repaint();
	        	 my_frame.setVisible(true);
	        	
	        }
	    }
	    private class ListenCancel implements ActionListener{
	        public void actionPerformed(ActionEvent e){
	                my_frame.remove(EventPostingPanel.this);
	                
	                my_frame.repaint();
	                my_frame.setVisible(true);
	        }
	    }
}
